//
//  AcountGoodsViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/30.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "AcountGoodsViewController.h"

#import "GoodListModel.h"
@interface AcountGoodsViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>

@property(nonatomic,strong)TB_TableView *ListtableView;

@property(nonatomic,strong)UISearchController *searchController;

@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,strong)NSMutableArray *orinArr;

@end

@implementation AcountGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.ListtableView];
    
    [self loadDatas];
    // Do any additional setup after loading the view.
}


-(void)viewWillAppear:(BOOL)animated
{
    
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColorRGBA(0x2E4361, 0.99)] forBarMetrics:UIBarMetricsDefault];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColorRGBA(0x2E4361, 1)] forBarMetrics:UIBarMetricsDefault];
    
}

-(NSMutableArray*)dataArr
{
    if (!_dataArr) {
        
        _dataArr=[NSMutableArray array];
        
    }
    
    return _dataArr;
}


-(NSMutableArray*)orinArr
{
    if (!_orinArr) {
        
        _orinArr=[NSMutableArray array];
        
    }
    
    return _orinArr;
}

-(TB_TableView*)ListtableView
{
    if (!_ListtableView) {
        
        _ListtableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        
        _ListtableView.estimatedRowHeight=50;
        
        _ListtableView.delegate=self;
        _ListtableView.dataSource=self;
        _ListtableView.tableFooterView=[[UIView alloc]init];
        _ListtableView.tableHeaderView=self.searchController.searchBar;
        
        [_ListtableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
    }
    
    return _ListtableView;
}


-(UISearchController*)searchController
{
    if (!_searchController) {

        // 初始化搜索控制器
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        
        self.definesPresentationContext = YES;
        // 设置代理
        _searchController.searchResultsUpdater = self;
        // 设置半透明背景，当设置当前视图控制器作为搜索结果的视图控制器时，要设为NO；
        _searchController.dimsBackgroundDuringPresentation =NO;
        // 因此导航栏
        _searchController.hidesNavigationBarDuringPresentation = NO;
        // 关掉自动大写锁定
        _searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        
        _searchController.searchBar.delegate=self;

        
        
    }
    
    return _searchController;
}


-(void)ChooseData:(GoodListData*)data
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadDatas
{
    
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/productList.json" andrequestTime:5 andParams:@{@"shopId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel shopId]],@"pageOn":@"0"} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        GoodListModel *InforModel=[GoodListModel mj_objectWithKeyValues:responseObject];

        if (responseStatus==1) {
            //请求成功
            if (InforModel.data.count==0) {
          
                self.Type=TBNODateType;
             
                [self.ListtableView reloadData];
                
            }else{
        
                [self.dataArr addObjectsFromArray:InforModel.data];
                
                [self.orinArr addObjectsFromArray:InforModel.data];
            
               
                [self.ListtableView reloadData];
                
            }
 
        }else if (responseStatus==0||responseStatus==2)
        {
          
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
    
    
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    
      [self.dataArr removeAllObjects];
    
    if (searchController.searchBar.text.length==0) {
        
        [self.dataArr addObjectsFromArray:self.orinArr];
        
        [self.ListtableView reloadData];
        
        return;
    }

    for (GoodListData *data in self.orinArr) {
  
        NSRange rang=[data.goodsName rangeOfString:searchController.searchBar.text];
        
        if (rang.location!=NSNotFound) {
            
            [self.dataArr addObject:data];
            
        }
        
    }
    
    
    
    [self.ListtableView reloadData];

    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
     [self.dataArr removeAllObjects];
    
    [self.dataArr addObjectsFromArray:self.orinArr];
    
    [self.ListtableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     GoodListData *data=self.dataArr[indexPath.row];
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text=data.goodsName;
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewAutomaticDimension;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodListData *data=self.dataArr[indexPath.row];
    
    [self ChooseData:data];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
